package com.jy.adscm.operation.lxcg.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.lxcg.bean.LxcgMaterial;
import com.jy.adscm.operation.lxcg.bean.QueryLxcgMaterial;
import com.jy.adscm.operation.lxcg.service.LxcgMaterialService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 零星基础物料表 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@RestController
@RequestMapping("/adscm/operation/lxcg/lxcgMaterial")
public class LxcgMaterialController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    LxcgMaterialService cgMaterialService;

    /**
     * 增加bom信息
     * @param lxcgMaterial
     * @param request
     * @return
     */
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody LxcgMaterial lxcgMaterial, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        lxcgMaterial.setCreator(token.getUser().getId().toString());
        lxcgMaterial.setCreateTime(new Date());
        lxcgMaterial.setEnable(1);
        boolean insert = cgMaterialService.insert(lxcgMaterial);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody LxcgMaterial lxcgMaterial, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        lxcgMaterial.setCreator(token.getUser().getId().toString());
        lxcgMaterial.setCreateTime(new Date());
        boolean insert = cgMaterialService.updateById(lxcgMaterial);
        return  new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryLxcgMaterial queryLxcgMaterial, HttpServletRequest request)throws AdscmSystemException {
        Page<LxcgMaterial> page=new Page(queryLxcgMaterial.getCurrentPage(),queryLxcgMaterial.getPageSize());
        page = cgMaterialService.selectLxcgMaterial(page,queryLxcgMaterial);
        return  new SimpleCoreResponse("200","修改成功",page);
    }

    @RequestMapping("selectByKeyWords")
    public SimpleCoreResponse selectByKeyWords( String name, HttpServletRequest request)throws AdscmSystemException {
        List<LxcgMaterial> LxcgMaterial=new ArrayList<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(name,null)){
            LxcgMaterial = cgMaterialService.selectByKeyWords(name);
        }
        return  new SimpleCoreResponse("200","查询成功",LxcgMaterial);
    }
    @RequestMapping("selectBySid/{sid}")
    public SimpleCoreResponse selectBySid(@PathVariable Integer sid, HttpServletRequest request)throws AdscmSystemException {
        List<LxcgMaterial> LxcgMaterial=new ArrayList<>();
            LxcgMaterial = cgMaterialService.selectBySid(sid);
        return  new SimpleCoreResponse("200","查询成功",LxcgMaterial);
    }
}

